<?php
namespace app\bancard\controller;
use think\facade\Cache;
use app\common\validate\DutyValidate;
use app\bancard\model\Duty as DutyModel;
class Duty extends Common {
    //推送key;
    protected $pushKey = [
        'action' => 'refresh',
        'params' => 'duty',
    ];
	//首页
	public function index(){
		$egetClass = $this->getClass();
		$this->assign('egetClass', json_encode($egetClass['data']));
		return $this->fetch();
	}
    public function getClassStudent(){
        $data = request()->param();
        $class_id = $data['class_id'];
        $info = $this->students_list($class_id);
        return show(1, '获取成功', $info, 200);
    }
	public function timetable(){
        $data = request()->param();
        $school_id = session('schoolInfo.id');
        $result = db('duty')->where(['class_id'=>$data['basicData'],'school_id'=>$school_id])->select();
        $student_id = array_column($result,'student_id');
        $data = $this->getStudents($student_id);
        $student_info = $data['data'];
        foreach ($result as $k=>$v){
            if(isset($student_info[$v['student_id']])){
                $result[$k]['student_name'] = $student_info[$v['student_id']]['name'];
                $result[$k]['student_avatar'] = $student_info[$v['student_id']]['avatar'];
            }
        }
        $data = [];
        foreach ($result as $k=>$v){
            switch ($v['week']){
                case 0:
                    $data['sunday'][] =$result[$k];
                    break;
                case 1:
                    $data['monday'][] =$result[$k];
                    break;
                case 2:
                    $data['tuesday'][] =$result[$k];
                    break;
                case 3:
                    $data['wednesday'][] =$result[$k];
                    break;
                case 4:
                    $data['thursday'][] =$result[$k];
                    break;
                case 5:
                    $data['friday'][] =$result[$k];
                    break;
                case 6:
                    $data['saturday'][] =$result[$k];
                    break;
            }
        }
        $this->assign('result',$data);
		return $this->fetch();
	}
    //添加值日生
    public function addDuty(){
        if(request()->isPost()){
            $data = request()->param();
            $data['created_at'] = time();
            $data['user_id'] = session('admin.id');
            $data['school_id'] = session('schoolInfo.id');
            $data['type'] = 1;
            $data['week'] = $data['attr_id'];
            $validate = new DutyValidate;
            if (!$validate->scene('add')->check($data)) {
                return show(1, $validate->getError(), 0 , 200);
            }else{
                Cache::clear('duty');
                $rest = (new DutyModel())->strict(false)->insert($data);
                if($rest){
                    //如果发布信息成功推送到相对于的设备上进行更新相对应的模块后期加入log日志判断是否发送成功
                    $this->socketPush($data['class_id'],$this->pushKey);
                }
                return show(0, '添加成功', $rest, 200);
            }
        }
    }
    //删除值日生
    public function delete(){
        Cache::clear('duty');
        $data = request()->param();
        $result = db('duty')->delete($data['id']);
        if($result){
            //如果发布信息成功推送到相对于的设备上进行更新相对应的模块后期加入log日志判断是否发送成功
            $this->socketPush($data['class_id'],$this->pushKey);
        }
        return show(0, '删除成功', $result, 200);
    }
}